<?php
namespace app\newapi\controller;

use think\Controller;
use app\common\model\Kefu as KefuModel;
use app\common\model\Csuser as CsuserModel;
// 常用回复
use app\common\model\Csuserwords as CsWordsModel;
use app\common\model\AuthorWxUser as AuthorWxUserModel;
use think\Session;
use think\Request;

class Kefu extends Controller{
    protected $kefu_model;
    protected $csuser_model;
    protected $cs_word_model;
    protected $author_wx_user_model;
    protected function _initialize(){
        parent::_initialize();
        $this->kefu_model = new KefuModel;
        $this->csuser_model = new CsuserModel;
        $this->cs_word_model = new CsWordsModel();
        $this->author_wx_user_model = new AuthorWxUserModel();
    }

    /**
     * 获取账号下的客服列表
     */
    public function SvCtmList($f_ids){
        $csuserList = $this->csuser_model
        ->alias('u')
        ->field('u.id,u.pid,u.auid,u.username,u.nickname,u.header_url,u.status,u.type,u.today_butt,u.online_butt,w.appid,w.wxname')
        ->whereIn('u.id',$f_ids)
        ->join('think_author_wx_user w','u.auid = w.id')
        ->select();
        if($csuserList){
            return $csuserList;
        }else{
            return false;
        }
    }

    /*
     * 获取常用回复
     * 携带id就返回对应的
     * */
    public function GetUsual($csid=0){
        $cswords = '';
        if($csid){
            $cswords0 = $this->cs_word_model->where(['cs_id'=>0,'is_delete'=>1])->select();
            $cswords1 = $this->cs_word_model->where(['cs_id'=>$csid,'is_delete'=>1])->select();
            $cswords = [
                'default'=>$cswords0,
                'person'=>$cswords1
            ];
        }else{
            $cswords = $this->cs_word_model->where(['cs_id'=>0,'is_delete'=>1])->select();
        }
//        var_dump($cswords);
        return json(['status' => 1,'msg' => $cswords]);
    }

    /*
     * 新增常用回复
     * */
    public function AddUsual(){
        if($this->request->isPost()) {
            $data = $this->request->post();
            //判断id是否存在
            $data_id = isset($data['id']) ? $data['id'] : 0;

            if($data_id){
                $result = $this->cs_word_model->where('id',$data_id)->update($data);
                if($result){
                    return json(['status' => 1,'msg' => '删除成功']);
                }else{
                    return json(['status' => -1,'msg' => '修改失败']);
                }
            }else{
                if($data['cs_id']){
                    $data['create_time'] = time();
                    $result = $this->cs_word_model->save($data);
                    if($result){
                        return json(['status' => 1,'msg' => $result]);
                    }else{
                        return json(['status' => 2,'msg' => '修改失败']);
                    }
                }else{
                    // 添加通用的常用回复
                    $data['create_time'] = time();
                    $result = $this->cs_word_model->save($data);
                    if($result){
                        return json(['status' => 1,'msg' => $result]);
                    }else{
                        return json(['status' => 2,'msg' => '修改失败']);
                    }
                }
            }

        }else{
//            $data = [
//                'cs_id' => 1,
//                'create_time' => time(),
//                'content' => '修改的常用回复',
//                'type' => '祛斑',
//                'sort' => 0,
//                'id' => 6
//            ];
//            $result = $this->cs_word_model->where('id',$data['id'])->update($data);
//            if($result){
//                return json(['status' => 1,'msg' => $result]);
//            }else{
//                return json(['status' => 2,'msg' => '修改失败']);
//            }
            return json(['status' => 404,'msg' => '非法请求']);
        }
    }

    /*
     * 删除常用回复
     * */
    public function deleteUsual($id){
        $result = $this->cs_word_model->where('id',$id)->update(['is_delete'=>2]);
        if($result){
            return json(['status' => 1,'msg' => '删除成功']);
        }else{
            return json(['status' => -1,'msg' => '修改失败']);
        }
    }
    
    /*
     * 新版常用回复功能,
     * 每个客服的常用回复用序列化存起来
     * */
    public function csuser_words($cs_id) {
        // 获取常用回复
        if(Request::instance()->isPost()){
            // 修改常用回复;
            $data = Request::instance()->post();
            $data['create_time'] = time();
            $has_word = $this->cs_word_model->where('cs_id',$cs_id)->find();
            $msg = '';
            if($has_word){
                $cs_word = $this->cs_word_model->where('cs_id',$cs_id)->update($data);
                $msg = '修改常用回复成功';
            }else {
                $cs_word = $this->cs_word_model->save($data);
                $msg = '新增常用回复成功';
            }
            return json([
                'status' => 201,
                'msg'    => $msg,
                'data'   => $cs_word
            ]);
        }else {
            // 获取常用回复
            $cs_word = $this->cs_word_model->where('cs_id',$cs_id)->select();
            return json([
                'status' => 200,
                'msg'    => '获取常用回复成功',
                'data'   => $cs_word
            ]);
        }
    }

    /*
     * 客服分配规则 1  优先平均分配给可分配在线客服 2 平均分配给所有可分配客服
     * id =>  session::get('wechat_info');
     * */
    public function distribution($id){
        $type = $this->author_wx_user_model->where('id',$id)->value('distribute_type');
        if($type){
            return json(['status' => 1,'type' => $type]);
        }else{
            return json(['status' => 404,'msg' => '请求失败']);
        }
    }

    /*
     * 修改分配规则
     * */
    public function changeDistribution(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = $this->author_wx_user_model->save(['distribute_type' => $data['type']],['id' => $data['id']]);
            if($result){
                return json(['status' => 1,'msg' => 'ok']);
            }else{
                return json(['status' => -1 , 'error' => 'update type error']);
            }
        }
    }






}